/*************************************************************************
	> File Name: client.cpp
	> Author: 
	> Mail: 
	> Created Time: Thu 24 Sep 2020 03:49:43 PM CST
 ************************************************************************/

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <vector>
#define MAXLINE 4096

using namespace std;

int main(int argc, char** argv){
    int connfd, n;
    char recvline[4096], sendline[4096];
    struct sockaddr_in servaddr;
    if(argc != 2){
        printf("usage: ./client [ipaddress]\n");
        return 0;
    }

    if((connfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
        return 0;
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(6666);
    
    if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
        printf("inet_pton error for %s\n", argv[1]);
        return 0;
    }

    if(connect(connfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
        printf("connect socket error: %s(errno: %d)\n", strerror(errno), errno);
        return 0;
    }

    ssize_t writeLen;
    char sendMsg[246888] = {0};
    int count = 0;
    while(1){
        count++;
        if(count == 5){
            return 0;
        }
        writeLen = write(connfd, sendMsg, sizeof(sendMsg));
        if(writeLen < 0){
            printf("write error: %s(errno: %d)\n", strerror(errno), errno);
            close(connfd);
            return 0;
        }
        else{
            printf("write success, writeLen: %d, sendMsg: %s\n", writeLen, sendMsg);
        }
    }

    close(connfd);
    return 0;
}
